<%-- 
    Document   : funciones2
    Created on : 13/04/2012, 08:20:01 PM
    Author     : Angel
--%>

<%@page import="java.security.MessageDigest"%>
<%@page import="java.sql.ResultSet"%>
<%@page language="Java" import="java.sql.*" %>
<jsp:useBean id="db" scope="request" class="conexion.DB" />
<jsp:useBean id="mail" scope="session" class="fantasy.SendMail" />
<%

String variable = "null";
   System.out.println("entro con login = " + request.getParameter("user"));
    
    
 if(request.getParameter("user") != null){
                            if(db.connect()){
                                ResultSet rs=  null;
                                ResultSet rsx=  null;
                                String user = "";
                                String pass="";
                                String status = "";
                                MessageDigest alg = MessageDigest.getInstance("MD5");
                                alg.reset(); 
                                alg.update(request.getParameter("pass").getBytes());
                                byte[] digest = alg.digest();
                                StringBuffer hashedpasswd = new StringBuffer();
                                String hx;
                                for (int i=0;i<digest.length;i++){
                                        hx =  Integer.toHexString(0xFF & digest[i]);
                                        //0x03 is equal to 0x3, but we need 0x03 for our md5sum
                                        if(hx.length() == 1){hx = "0" + hx;}
                                        hashedpasswd.append(hx);
                                }
                                String password = hashedpasswd.toString();
                                String login_usuarios = request.getParameter("user");
                               try{
                                rs = db.seleccionar("email_user_login,password_login,status_login", "login", "1");

                                while(rs.next()){
                                   user = rs.getString("email_user_login");
                                   pass = rs.getString("password_login");
                                   status = rs.getString("status_login");
                                   if(user.equals(login_usuarios) && password.equals(pass) && status.equals("1") ){
                                       
                                           rsx = db.seleccionar("COUNT(*) as conteo", "User", "email_user = '"+user+"' AND id_role_user = " + request.getParameter("tipo_usuario"));
                                           while(rsx.next()){
                                               if(rsx.getInt(1) == 1){
                                                   variable = "1";
                                               }
                                           }
                                   }
                                }
                                }catch(Exception e){
                                   variable = "null";
                                }
                               if(!variable.equals("1") || variable.equals("")){
                                    variable ="null";
                               }
                            }else{
                                variable ="null";
                            }
    }else{
        variable ="null";
    }   
    
    
       
   if(variable.equals("1")){//login correcto
       
       int result;
       result = db.actualizar("connected_login", "1", "login", "email_user_login = '"+request.getParameter("user").toString()+"'");
       
       
       if(request.getParameter("tipo_usuario").toString().equals("1")){//administrador
           session.setAttribute("admin",request.getParameter("user")); 
           response.sendRedirect("../ADMINISTRADOR/inicio.jsp");
       }else{//usuario
           session.setAttribute("usuario",request.getParameter("user"));
           response.sendRedirect("../USUARIO/inicio.jsp");
       }
   }else{//login incorrecto
           
   }
    
    
    %>
    
    
    <%@page import="java.util.Date"%>
<%@page import="java.util.Calendar"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>ANALISIS Y DISEÑO 2</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="../CSS/default.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<!-- start header -->

<div id="header">
<img src="../imagenes/usac1.jpg"/>        
	<h1><a href="#">FANTASY FOOTBALL</a></h1>
	<p><a href="#">USAC - AYD2</a></p>
        
       

</div>
<!-- end header -->
<!-- star menu -->
<div id="menu">
	<ul>
		<li class="current_page_item"><a href="../index.jsp">INICIO</a></li>
		<li><a href="liga.jsp">LIGAS</a></li>
		<li><a href="equipo.jsp">EQUIPOS</a></li>                
                <li><a href="jugador.jsp">JUGADORES</a></li>
	</ul>
</div>
<!-- end menu -->
<!-- start page -->
<div id="page">
	<!-- start content -->
	<div id="content">
		<div class="post">
			<div class="title">
				<h2><a href="liga.jsp">Usuario Incorrecto!</a></h2>
				<p><small>fase 3 </small></p>
			</div>
			<div class="entry">
                            <p>Ingrese un Usuario Correcto.</p>					
                            
			</div>			
		</div>
		<div class="post">
			<div class="entry">
				<blockquote>
				</blockquote>
			</div>
			<p class="links">  <a href="#" class="comments">Inicio</a> </p>
                        <CENTER>
                         <H2></H2>
                         <% 
                         Date fecha = new Date();
                         out.print("<b>Hoy es:</b> ");
                         out.print(fecha.getDate());
                         out.print(" / ");
                         out.print(fecha.getMonth()+1);
                         out.print(" / ");
                         out.print(fecha.getYear()+1900);
                         %>
                         </CENTER> 
		</div>
	</div>
	<!-- end content -->
	<!-- start sidebar -->
	<div id="sidebar">
		<div id="search">
			<h2>Login</h2>
			<form id="searchform" method="get" action="">
                            <div>
				<fieldset>
                               <center>
                                   <a style="color:black; font-size:35px ;" href="../index.jsp">Login</a>
                                   <br><br>
                                <li><a href="registro.jsp">Registrate</a></li>
                               </center>
				</fieldset>
                            </div>
			</form>
		</div>
		<ul>
			<li id="categories">
				<h2>MENU</h2>
				<ul>
					<li><a href="liga.jsp">LIGAS</a>  </li>
					<li><a href="equipo.jsp">EQUIPOS</a> </li>
                                        <li><a href="jugador.jsp">JUGADORES</a> </li>
                                        <img src="../imagenes/pelota.png" alt="" />
				</ul>
			</li>
						
		</ul>
	</div>
	<!-- end sidebar -->
	<div id="extra" style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
	<p class="legal"> &copy;2012 Usac. All Rights Reserved.
		&nbsp;&nbsp;&bull;&nbsp;&nbsp;		
</div>
<!-- end footer -->
</body>
</html>
